﻿using Enums;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using XF.Common;
using XF.ExControls;

namespace KYBM
{
    public partial class FrmSTOList : Form
    {
        XF.BLL.Bill_STOHead bllHead = new XF.BLL.Bill_STOHead();
        XF.BLL.Bill_STOItems bll = new XF.BLL.Bill_STOItems();
        DateTimePicker dtpDateStart = new DateTimePicker();
        DateTimePicker dtpDateEnd = new DateTimePicker();
        int iVerticalScrollIndex = 0;

        public FrmSTOList()
        {
            InitializeComponent();
        }

        private void FrmSTOList_Load(object sender, EventArgs e)
        {
            xfDataGridView1.AutoGenerateColumns = false;
            dtpDateStart.Width = dtpDateEnd.Width = 120;
            dtpDateStart.Format = dtpDateEnd.Format = DateTimePickerFormat.Custom;
            dtpDateStart.CustomFormat = dtpDateEnd.CustomFormat = "yyyy-MM-dd";
            ToolStripControlHost item = new ToolStripControlHost(dtpDateEnd);
            toolStrip1.Items.Insert(9, item);
            item = new ToolStripControlHost(dtpDateStart);
            toolStrip1.Items.Insert(8, item);
            ColStatus.DisplayMember = MessageText.LISTITEM_TEXT;
            ColStatus.ValueMember = MessageText.LISTITEM_VALUE;
            ColStatus.DataSource = GenerateData.GetSTOStatus(false);
            ColUnit.DisplayMember = MessageText.LISTITEM_TEXT;
            ColUnit.ValueMember = MessageText.LISTITEM_VALUE;
            ColUnit.DataSource = GenerateData.GetUnit(false);
            BindData();
        }

        private void tsBtnSearch_Click(object sender, EventArgs e)
        {
            pagerControl1.PageIndex = 1;
            BindData();
        }

        private void BindData()
        {
            int sum = 0;
            string strWhere = string.Format(" and OrderDate >= '{0}' and OrderDate < '{1}'", dtpDateStart.Value.ToString(MessageText.FORMAT_DATE), dtpDateEnd.Value.AddDays(1).ToString(MessageText.FORMAT_DATE));
            if (!string.IsNullOrEmpty(tsTbCustomer.Text.Trim()))
            {
                strWhere += " and (CustomerCode like '%" + tsTbCustomer.Text.Trim() + "%' or CustomerName like '%" + tsTbCustomer.Text + "%')";
            }
            if (!string.IsNullOrEmpty(tsTbMaterielName.Text.Trim()))
            {
                strWhere += " and MaterielName like '%" + tsTbMaterielName.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(tsTbMaterielModel.Text.Trim()))
            {
                strWhere += " and MaterielModel like '%" + tsTbMaterielModel.Text.Trim() + "%'";
            }
            if (!string.IsNullOrEmpty(tsTbSupplier.Text.Trim()))
            {
                strWhere += " and Supplier like '%" + tsTbSupplier.Text.Trim() + "%'";
            }
            List<XF.Model.Bill_STOItems> models = bll.DataTableToList(bll.GetDetailListByPage(strWhere, " OrderDate Desc,CreateDate Desc ", pagerControl1.PageIndex, pagerControl1.PageSize, ref sum));
            xfDataGridView1.Rows.Clear();
            foreach(XF.Model.Bill_STOItems model in models)
            {
                int count = xfDataGridView1.RowCount;
                xfDataGridView1.Rows.Add();
                xfDataGridView1.Rows[count].Cells[ColOrderId.Name].Value = model.OrderId;
                xfDataGridView1.Rows[count].Cells[ColOrderCode.Name].Value = model.OrderCode;
                xfDataGridView1.Rows[count].Cells[ColOrderDate.Name].Value = model.OrderDate;
                xfDataGridView1.Rows[count].Cells[ColCreateDate.Name].Value = model.CreateDate;
                xfDataGridView1.Rows[count].Cells[ColCustomerId.Name].Value = model.CustomerId;
                xfDataGridView1.Rows[count].Cells[ColCustomerCode.Name].Value = model.CustomerCode;
                xfDataGridView1.Rows[count].Cells[ColCustomerName.Name].Value = model.CustomerName;
                xfDataGridView1.Rows[count].Cells[ColStatus.Name].Value = model.Status;
                xfDataGridView1.Rows[count].Cells[ColId.Name].Value = model.Id;
                xfDataGridView1.Rows[count].Cells[ColMaterielId.Name].Value = model.MaterielId;
                xfDataGridView1.Rows[count].Cells[ColMaterielName.Name].Value = model.MaterielName;
                xfDataGridView1.Rows[count].Cells[ColMaterielModel.Name].Value = model.MaterielModel;
                xfDataGridView1.Rows[count].Cells[ColUnit.Name].Value = model.Unit;
                xfDataGridView1.Rows[count].Cells[ColQty.Name].Value = model.Qty;
                xfDataGridView1.Rows[count].Cells[ColReamrk.Name].Value = model.Remark;
                xfDataGridView1.Rows[count].Cells[ColOrderRemark.Name].Value = model.OrderRemark;
                xfDataGridView1.Rows[count].Cells[ColSupplier.Name].Value = model.Supplier;
                if(model.Status == 2 || model.DoQty >= model.Qty)
                {
                    xfDataGridView1.Rows[count].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#5FAEE3");
                }
                else if(model.Status == 1)
                {
                    xfDataGridView1.Rows[count].DefaultCellStyle.BackColor = ColorTranslator.FromHtml("#DCDCDC");
                }
            }
            pagerControl1.DrawControl(sum);
        }

        private void pagerControl1_OnPageChanged(object sender, EventArgs e)
        {
            BindData();
        }

        private void tsBtnCancel_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count > 0)
            {
                string sIDs = string.Empty;
                string sID;
                //构建IDs
                foreach (int i in indexs)
                {
                    if(zDataConverter.ToInt(xfDataGridView1.Rows[i].Cells[ColStatus.Name].Value) != 0)
                    {
                        QQMessageBox.Show(
                        this,
                        string.Format(MessageText.TIP_ERROR_UNNORMAL, i + 1),
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                        return;
                    }
                    sID = zDataConverter.ToString(xfDataGridView1.Rows[i].Cells[ColId.Name].Value);
                    if (sID.Length > 0)
                    {
                        sIDs += MessageText.KEY_COMMA + sID;
                    }
                }
                if (sIDs.Length > 0)
                {
                    //执行数据库删除
                    if (!bll.CancelList(sIDs.Substring(1)))
                    {
                        QQMessageBox.Show(
                        this,
                        MessageText.TIP_ERROR_CANCEL,
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                        return;
                    }
                }
                BindData();
                xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_CANCEL,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.OK,
                            QQMessageBoxButtons.OK);

            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_NULL,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnRecovery_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count > 0)
            {
                string sIDs = string.Empty;
                string sID;
                //构建删除ID
                foreach (int i in indexs)
                {
                    if (zDataConverter.ToInt(xfDataGridView1.Rows[i].Cells[ColStatus.Name].Value) == 0)
                    {
                        QQMessageBox.Show(
                        this,
                        string.Format(MessageText.TIP_ERROR_ISRECOVERY, i + 1),
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                        return;
                    }
                    sID = zDataConverter.ToString(xfDataGridView1.Rows[i].Cells[ColId.Name].Value);
                    if (sID.Length > 0)
                    {
                        sIDs += MessageText.KEY_COMMA + sID;
                    }
                }
                if (sIDs.Length > 0)
                {
                    //执行数据库删除
                    if (!bll.RecoveryList(sIDs.Substring(1)))
                    {
                        QQMessageBox.Show(
                        this,
                        MessageText.TIP_ERROR_RECOVERY,
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                        return;
                    }
                }
                BindData();
                xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_RECOVERY,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.OK,
                            QQMessageBoxButtons.OK);

            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_NULL,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnDetail_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                int iId = zDataConverter.ToInt(xfDataGridView1.Rows[indexs[0]].Cells[ColCustomerId.Name].Value);
                FrmSTOHistory frmSTOHistory = new FrmSTOHistory(iId);
                frmSTOHistory.ShowDialog();
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnExecute_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if(indexs.Count >0)
            {
                int customerId = zDataConverter.ToInt(xfDataGridView1.Rows[indexs[0]].Cells[ColCustomerId.Name].Value);
                string sItemIds = string.Empty;
                foreach (int index in indexs)
                {
                    if (customerId != zDataConverter.ToInt(xfDataGridView1.Rows[index].Cells[ColCustomerId.Name].Value))
                    {
                        QQMessageBox.Show(
                            this,
                            MessageText.CHECK_ERROR_STO_CUSTOMER_UNIQUE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
                        return;
                    }
                    sItemIds += "," + zDataConverter.ToInt(xfDataGridView1.Rows[index].Cells[ColId.Name].Value);
                }
                FrmSTO2DTO frmSTO2DTO = new FrmSTO2DTO(customerId, sItemIds.Substring(1));
                if (frmSTO2DTO.ShowDialog() == DialogResult.OK)
                {
                    BindData();
                    xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                }
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_NULL,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnComplete_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count > 0)
            {
                string sIDs = string.Empty;
                string sID;
                //构建删除ID
                foreach (int i in indexs)
                {
                    if (zDataConverter.ToInt(xfDataGridView1.Rows[i].Cells[ColStatus.Name].Value) != 0)
                    {
                        QQMessageBox.Show(
                        this,
                        string.Format(MessageText.TIP_ERROR_UNNORMAL, i + 1),
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                        return;
                    }
                    sID = zDataConverter.ToString(xfDataGridView1.Rows[i].Cells[ColId.Name].Value);
                    if (sID.Length > 0)
                    {
                        sIDs += MessageText.KEY_COMMA + sID;
                    }
                }
                if (sIDs.Length > 0)
                {
                    //执行数据库删除
                    if (!bll.CompleteList(sIDs.Substring(1)))
                    {
                        QQMessageBox.Show(
                        this,
                        MessageText.TIP_ERROR_RECOVERY,
                        MessageText.MESSAGEBOX_CAPTION_ERROR,
                        QQMessageBoxIcon.Error,
                        QQMessageBoxButtons.OK);
                        return;
                    }
                }
                BindData();
                xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_SUCCESS_RECOVERY,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.OK,
                            QQMessageBoxButtons.OK);

            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_NULL,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnPlace_Click(object sender, EventArgs e)
        {
            FrmSTOPlace frmSTOPlace = new FrmSTOPlace();
            if (frmSTOPlace.ShowDialog() == DialogResult.OK)
            {
                BindData();
                if (xfDataGridView1.Rows.Count > 0)
                {
                    xfDataGridView1.FirstDisplayedScrollingRowIndex = iVerticalScrollIndex;
                }
            }
        }

        private void xfDataGridView1_Scroll(object sender, ScrollEventArgs e)
        {
            if (e.ScrollOrientation == ScrollOrientation.VerticalScroll)
            {
                iVerticalScrollIndex = e.NewValue;
            }
        }

        private void FrmSTOList_KeyDown(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.F1:
                    tsBtnDetail_Click(this, EventArgs.Empty);
                    break;
                case Keys.F2:
                    tsBtnPlace_Click(this, EventArgs.Empty);
                    break;
                case Keys.F3:
                    tsBtnLook_Click(this, EventArgs.Empty);
                    break;
                case Keys.F4:
                    tsBtnCopy_Click(this, EventArgs.Empty);
                    break;
                case Keys.F5:
                    tsBtnComplete_Click(this, EventArgs.Empty);
                    break;
                case Keys.F6:
                    tsBtnExecute_Click(this, EventArgs.Empty);
                    break;
                case Keys.F7:
                    tsBtnRecovery_Click(this, EventArgs.Empty);
                    break;
                case Keys.F8:
                    tsBtnCancel_Click(this, EventArgs.Empty);
                    break;

            }
        }

        private void tsBtnResetSearch_Click(object sender, EventArgs e)
        {
            dtpDateStart.Value = DateTime.Now.AddMonths(-1);
            dtpDateEnd.Value = DateTime.Now.Date;
            pagerControl1.PageIndex = 1;
            BindData();
        }

        private void tsBtnLook_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                int iId = zDataConverter.ToInt(xfDataGridView1.Rows[indexs[0]].Cells[ColOrderId.Name].Value);
                FrmSTODetail frmSTODetail = new FrmSTODetail(iId);
                frmSTODetail.ShowDialog();
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }

        private void tsBtnCopy_Click(object sender, EventArgs e)
        {
            List<int> indexs = xfDataGridView1.GetSelectedRowsIndex();
            if (indexs.Count == 1)
            {
                int iId = zDataConverter.ToInt(xfDataGridView1.Rows[indexs[0]].Cells[ColOrderId.Name].Value);
                FrmSTOCopy frmSTOCopy = new FrmSTOCopy(iId);
                frmSTOCopy.ShowDialog();
            }
            else
            {
                QQMessageBox.Show(
                            this,
                            MessageText.TIP_ERROR_SELECTCOUNT_ONE,
                            MessageText.MESSAGEBOX_CAPTION_TIP,
                            QQMessageBoxIcon.Information,
                            QQMessageBoxButtons.OK);
            }
        }
    }
}
